﻿
// Condition: A given matrix of strings of size N x M. Write a program that finds the longest sequene ( line, column or diagonal)
//            of equal strings in the matrix. 

using System;


class SeqInMatrix
{
    static void Main()
    {

        // define the size of matrix
        Console.Write("Enter number of rows n=");
        int n = int.Parse(Console.ReadLine());
        Console.Write("Enter number of columns m=");
        int m = int.Parse(Console.ReadLine());
        string[,] matrix = new string[n, m];
            
        // read from console elements of the matrix

        for (int row = 0; row < matrix.GetLength(0); row++)
        {
            for (int col = 0; col < matrix.GetLength(1); col++)
            {
                Console.Write("Enter element[{0},{1}]:",row,col);
                matrix[row, col] = (Console.ReadLine());
            }
        }

        Console.WriteLine();
        Console.WriteLine("Original matrix is:");
            
        // print the matrix[n,m]

        for (int irow = 0; irow < n; irow++)
        {
            for (int icol = 0; icol < m; icol++)
            {
                Console.Write(matrix[irow, icol] + " ");
            }
            Console.WriteLine();
        }
        Console.WriteLine();
         
        
        int p = 1;
        int count = 1;
        int maxCount = int.MinValue;
        string bestMatrix=null;


        // row
        
        for (int row = 0; row < n; row++)
        {
            for (int col = 0; col < m-1; col++)
            {
                count = 1;
                p = 1;
                while (col+p< m &&  matrix[row,col]==matrix[row,col+p])
                {
                    p++;
                    count++;
                    if (count >= maxCount)
                    {
                        maxCount = count;
                        bestMatrix = matrix[row, col];
                    }
                    
                }
            }
        }
        

        // column

        for (int col = 0; col < m; col++)
        {
            for (int row = 0; row < n-1; row++)
            {
                count = 1;
                p = 1;
                while ( row+p<n && matrix[row,col]==matrix[row+p,col])
                {
                    p++;
                    count++;
                    if (count >= maxCount)
                    {
                        maxCount = count;
                        bestMatrix = matrix[row, col];
                    }
                    
                }



            }
        }

        // diagonal right

        for (int row = 0; row < n-1; row++)
        {
            for (int col = 0; col < m-1; col++)
            {
                count = 1;
                p = 1;
                while ( row+p<n && col+p<m && matrix[row, col] == matrix[row + p, col + p])
                {
                    p++;
                    count++;
                    if (count >= maxCount)
                    {
                        maxCount = count;
                        bestMatrix = matrix[row, col];
                    }
                }
                
            }
        }
     
        // diagonal left

        for (int row = 0; row < n-1; row++)
        {
            for (int col = 1; col < m; col++)
            {
                count = 1;
                p = 1;
                while (row + p < n && col - p > 0 && matrix[row, col] == matrix[row + p, col - p])
                {
                    p++;
                    count++;
                    if (count >= maxCount)
                    {
                        maxCount = count;
                        bestMatrix = matrix[row, col];
                    }

                }
            }
        }


        // print the result

        Console.Write("The longest sequence of equal strings is: " );
        for (int i = 0; i < maxCount; i++)
        {
            Console.Write("{0}",bestMatrix );
            if (i == maxCount-1)
            {
                break;
            }
            Console.Write(", ");
        }
        Console.WriteLine();
    }
}

